<?
	class PLS { // php login system

		const VERSION = '1.0';

		// -------------------- SETUP --------------------
		public static function install(){
			$sql = "

			CREATE TABLE  `user` (
			 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			 `username` VARCHAR( 20 ) NOT NULL ,
			 `digest` VARCHAR( 40 ) NOT NULL ,
			 `salt` INT NOT NULL ,
			 `pepper` INT NOT NULL ,
			 `fail_login` INT( 1 ) NOT NULL DEFAULT 0 ,
			 `last_login` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
			 `status` VARCHAR( 1 ) NOT NULL DEFAULT  'A'
			) ENGINE = MYISAM ;

			";
			mysql_query($sql);
		}
		public static function uninstall(){
			$sql = "

			DROP TABLE `user`;

			";
			mysql_query($sql);
		}
		public static function is_installed(){
			return mysql_query('select 1 from `user`') ? true : false;
		}

		// -------------------- USER --------------------
		public static function add_user($username, $password){
			$salt = rand();
			$pepper = rand();
			$digest = self::make_a_digest($password, $salt, $pepper);
			$last_login = time();

			$sql = "

			INSERT INTO `user` ( `username`, `digest`, `salt`, `pepper` ) 
			VALUES ( '$username', '$digest', $salt, $pepper );

			";
			mysql_query($sql) or die(mysql_error());
		}
		public static function remove_user($id){
			$sql = "

			DELETE FROM `user` WHERE `id` = $id LIMIT 1;

			";
			mysql_query($sql) or die(mysql_error());
		}
		public static function block_user($id){
			$sql = "

			UPDATE `user` SET `status` = 'C' WHERE `id` = $id;

			";
			mysql_query($sql) or die(mysql_error());
		}
		public static function unblock_user($id){
			$sql = "

			UPDATE `user` SET `status` = 'R' WHERE `id` = $id;

			";
			mysql_query($sql) or die(mysql_error());
		}

		// -------------------- LOGIN --------------------
		public static function login($username, $password){
			$success = false;
			$sql = "

			SELECT `digest`, `salt`, `pepper`, `fail_login`, `last_login`, `status` FROM `user` WHERE `username` = '$username';

			";
			$result = mysql_query($sql) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$status     = $row['status'];
			$fail_login = $row['fail_login'];
			$last_login = $row['last_login'];
			$digest = $row['digest'];
			$salt   = $row['salt'];
			$pepper = $row['pepper'];

			$mysql_date = date('Y-m-d H:i:s');

			if($row){
				if($status == 'C'){ // user close ?
					$message = 'User is closed.';
				}
				else if($last_login > $mysql_date){ // login limit
 					$message = "Login is limitd, please login again on $last_login.";
				}
				else { // compare digest
					$new_digest = self::make_a_digest($password, $salt, $pepper);
					if($digest == $new_digest ){
						session_register('PLS_USERNAME');
						session_register('PLS_LAST_LOGIN');
						$_SESSION['PLS_USERNAME']   = $username;
						$_SESSION['PLS_LAST_LOGIN'] = time();
						$sql = "

						UPDATE `user` SET `fail_login` = 0, `last_login` = '$mysql_date' WHERE `username` = '$username';

						";
						mysql_query($sql) or die(mysql_error());
						$message = 'Login success.';
						$success = true;
					}
					else {
						$fail_login += 1;
						$limit_date = $mysql_date;
						if($fail_login == 3){
							$fail_login = 0;
							$limit_date = date('Y-m-d H:i:s', time() + 30*60);
						}
						$sql = "

						UPDATE `user` SET `fail_login` = $fail_login, `last_login` = '$limit_date' WHERE `username` = '$username';

						";
						mysql_query($sql) or die(mysql_error());
						$message = 'Login failed.';
					}
				}
			}
			else {
				$message = 'Username not found.';
			}
			return array( 'success' => $success, 'message' => $message );
		}
		public static function logout(){
			session_unregister('PLS_USERNAME');
			session_unregister('PLS_LAST_LOGIN');
		}
		public static function is_login(){
			// TODO enough check ?
			return isset($_SESSION['PLS_USERNAME']) &&
					isset($_SESSION['PLS_USERNAME']);
		}

		// -------------------- DIGEST --------------------
		private function make_a_digest($password, $salt, $pepper){
			return sha1($pepper.$password.$salt);
		}
	}
?>
